package com.springcloud.market.order.service.impl;

import com.springcloud.market.order.feign.StockFeignService;
import com.springcloud.market.order.mapper.OrderMapper;
import com.springcloud.market.order.pojo.Order;
import com.springcloud.market.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class OrderServiceImpl implements OrderService {
 @Autowired
 private OrderMapper orderMapper;

 @Autowired
 private StockFeignService stockFeignService;

    @Autowired
    private OrderTimeoutSender orderTimeoutSender;

 @Transactional
 @Override
 public Order create(Order order) {
    orderMapper.insert(order);
    String msg = stockFeignService.seataReduce(order.getProductId());
    System.out.println(msg);
     // 故意异常：余数不能为0
//    int i = 1/0;
    return order;
  }

    @Override
    public Order createOrder() {
        Integer orderId = 202; // 假设这是生成的订单ID

        // 发送延迟消息
        try {
            orderTimeoutSender.sendOrderTimeoutMessage(orderId.toString());
        } catch (Exception e) {
            // 记录日志，但通常不应该回滚主业务
            System.err.println("发送超时消息失败: " + e.getMessage());
        }

        // 返回订单信息。可在订单中加上状态字段，如未支付则取消订单，如支付则执行已支付的逻辑
        // （发送已支付消息，由已支付消费者执行）
        return new Order(orderId, 203);
    }
}

